/**
 * Created by lim on 2017/3/21.
 */
var curProp;
var totalPages;
var curPage;
var curFunc;

/**
 * 判断是否为空
 * @param object
 * @returns {boolean}
 * @constructor
 */
function IsBlank(object) {
    if (typeof(object) == "undefined" || object == null || object == "" || object == "null") {
        return true;
    }
}
/**
 * 添加0
 * @param iNum
 * @returns {string}
 */
function addZero(iNum) {
    return iNum < 10 ? "0" + iNum.toString() : iNum.toString();
}

//首页
function firstPage() {
    curProp.pageNo = 1;
    FlipTablePage(curProp, curFunc);
}
//上一页
function prePage() {
    if (curPage > 1) {
        curProp.pageNo = curPage - 1;
        FlipTablePage(curProp, curFunc);
    }

}
//下一页
function nextPage() {
    if (curPage < totalPages) {
        curProp.pageNo = curPage + 1;
        FlipTablePage(curProp, curFunc);
    }
}
//尾页
function lastPage() {
    curProp.pageNo = totalPages;
    FlipTablePage(curProp, curFunc);
}
//按给定页码展示数据
function searchPage(event) {
    if (event.keyCode == 13 || window.event.keyCode == 13) {
        var pager = $("#searchPageInput").val();
        if (pager == "") {
            alert("请填写页码");
            return false;
        }
        var reg = /^[1-9]*[1-9][0-9]*$/;
        if (!reg.test(pager)) {
            alert("页码位数字,请正确填写页码");
            return false;
        }
        ;
        if (pager < 1 || pager > totalPages) {
            alert("页码范围不正确，请正确填写页码");
            return false;
        }
        curProp.pageNo = pager;
        FlipTablePage(curProp, jvectorMapPage);

    }
}

var jvectorMapPage = function (contentContainer, headerContainer, bottomContainer, url, data, tableClass, tableColNames, tableColModel, displayAll) {
    var tableHtml = "";
    //使用getJson方法获取Json数据
    jQuery.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        success: function (jsonMap) {
            if (jsonMap.isSuccess) {
                if (jsonMap.title != null && jsonMap.title != "") {
                    tableHtml += "<p class='title'>" + jsonMap.title + "</p>";
                }
                tableHtml += "<table width='100%' class='" + tableClass + "' cellpadding='0' cellspacing='0'><thead><tr>";
                var thread = "";
                for (var i = 0; i < tableColNames.length; i++) {
                    if (tableColModel[i].width == -1) {
                        thread += "<th class='hidden' style='font-size:12px'>" + tableColNames[i] + "</th>";
                    } else if (tableColModel[i].width == 0) {
                        thread += "<th class='center' style='font-size:12px'>" + tableColNames[i] + "</th>";
                    } else {
                        thread += "<th class='center' style='font-size:12px' width=" + tableColModel[i].width + ">" + tableColNames[i] + "</th>";
                    }
                }
                tableHtml += thread + "</tr></thead><tbody>";

                var tableBody = "";
                if (jsonMap.dataList == null || jsonMap.dataList == "" || jsonMap.dataList.length == 0) {
                    var tip = "没有查询到数据！";
                    tableBody = "<tr><td colspan='" + tableColNames.length + "'><font style='color: red;'>" + tip + "</font></td></tr>";
                } else {
                    $(jsonMap.dataList).each(function (i, tableData) {
                        var trClass = tableData.trClass;
                        if (typeof(trClass) != "undefined") {
                            tableBody += "<tr class='" + trClass + "'>";
                        } else {
                            tableBody += "<tr>";
                        }
                        for (var j = 0; j < tableColModel.length; j++) {
                            var tdContent = tableData[tableColModel[j].name];
                            if (tdContent == null || tdContent == "null") {
                                tdContent = "&nbsp;";
                            }
                            var typeContent = "";
                            if (tableColModel[j].type == 1) {   //checkbox
                                typeContent = "<label class='pos-rel'><input type='checkbox' class='ace' name='checkId' value='" + tdContent + "'> <span class='lbl'></span></label>";
                            } else if (tableColModel[j].type == 2) { //time
                                if (tdContent != "" && tdContent != "&nbsp;") {
                                    var dt = new Date(tdContent);
                                    typeContent = dt.getFullYear().toString() + "/" + addZero(dt.getMonth()) + "/" + addZero(dt.getDay()) + " "
                                        + addZero(dt.getHours()) + ":" + addZero(dt.getMinutes()) + ":" + addZero(dt.getSeconds());
                                } else {
                                    typeContent = tdContent;
                                }
                            } else if (tableColModel[j].type == 3) { //status
                                if (tdContent == 0) {
                                    typeContent = "<span class='label label-success arrowed-in arrowed-in-right'>正常</span>";
                                } else if (tdContent == 1) {
                                    typeContent = "<span class='label arrowed'><s>停用</s></span>";
                                } else if (tdContent == 2) {
                                    typeContent = "<span class='label label-danger arrowed'>锁定</span>";
                                } else if (tdContent == 3) {
                                    typeContent = "<span class='label label-info arrowed-right arrowed-in'>未激活</span>";
                                }
                            } else if (tableColModel[j].type == 4) {
                                if (tdContent != "&nbsp;") {
                                    typeContent = "<a onclick='ClickLink(" + tdContent + ");'>" + tableColNames[j] + "</a>";
                                }
                            }
                            var tdShow = "";
                            if (!(tableColModel[j].type == 1 || tableColModel[j].type == 2 || tableColModel[j].type == 3 || tableColModel[j].type == 4)) {
                                tdShow = tdContent;
                            }
                            if (typeof(tableColModel[j].align) == "undefined") {
                                if (tableColModel[j].width == -1) {
                                    tableBody += "<td class='hidden'>" + tdShow + typeContent + "</td>";
                                } else {
                                    tableBody += "<td>" + tdShow + typeContent + "</td>";
                                }
                            } else {
                                var newTdContent = tdShow;
                                if (!displayAll) {
                                    newTdContent = tdShow.length > 45 ? tdShow.substring(0, 45) + "..." : tdShow;
                                }
                                if (tableColModel[j].width == -1) {
                                    tableBody += "<td class='hidden' style='font-size:12px;text-align:" + tableColModel[j].align + "' title=" + tdContent + ">" + newTdContent + typeContent + "</td>";
                                } else {
                                    tableBody += "<td style='font-size:12px;text-align:" + tableColModel[j].align + "' title=" + tdContent + ">" + newTdContent + typeContent + "</td>";
                                }
                            }
                        }
                        tableBody += "</tr>";
                    });
                }
                tableHtml += tableBody + "</tbody></table>";
                contentContainer.empty();
                contentContainer.html(tableHtml);
                curPage = jsonMap.curPage;
                totalPages = jsonMap.totalPages;
                pageBottom(bottomContainer, curPage, jsonMap.totalCount, totalPages);
            }
            else {
                layer.msg('错误码 ' + jsonMap.msg, {icon: 5, time: 1000});
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status == 401) {
                window.location.href = XMLHttpRequest.responseText;
                return;
            }
            alert(XMLHttpRequest.status);
            layer.msg('出现异常!', {icon: 5, time: 1000});
            return;
        }
    });
}

var pageBottom = function (tableContainer, curPage, totalCounts, totalPages) {
    var pagerHtml = "<table cellpadding='0' cellspacing='0' align='right'><tr><td style='font-size: 12px;'>";

    var inputIsDisable = "";
    if (totalCounts <= 0) {
        inputIsDisable = "disabled = 'disabled'";
    }
    pagerHtml += "<input style='text-align:center; width: 30px; height: 24px; font-size: 12px;' id='searchPageInput' " + inputIsDisable + " onkeypress='searchPage(event);' type='text' value='" + curPage + "'> 页/ " + totalPages + "页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

    if (curPage == 1 || totalCounts <= 0) {
        pagerHtml += "首页&nbsp;&nbsp;上一页&nbsp;&nbsp;";
    } else {
        pagerHtml += "<a onclick='firstPage();'>首页</a>&nbsp;&nbsp;<a onclick='prePage();'>上一页</a>&nbsp;&nbsp;";
    }

    if (curPage == totalPages || totalCounts <= 0) {
        pagerHtml += "下一页&nbsp;&nbsp;尾页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    } else {
        pagerHtml += "<a onclick='nextPage();'>下一页</a>&nbsp;&nbsp;<a onclick='lastPage();'>尾页</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    }


    pagerHtml += "</td></tr></table>";

    tableContainer.empty();
    tableContainer.html(pagerHtml);
}


function FlipTablePage(Opts, Func) {
    curProp = Opts;
    var pageSize = Opts.pageSize;                                    //每页显示数量
    var curPageNo = Opts.curPageNo;                                  //当前页码
    var tableId = Opts.divId;                                        //table 表格div
    var pageBottom = Opts.pager;                                    //table 分页div
    var pageHeader = Opts.pageHeader;
    var url = Opts.url;                                              //url
    var data = Opts.data;                                            //数据
    var divWidth = Opts.Width;                                       //表格可视区域宽度
    var divHeight = Opts.Height;                                     //表格可视区域高度
    var tbColModel = Opts.colModel;                                  //列样式
    var tbColName = Opts.colNames;                                    //列名称
    var displayAll = Opts.tdDisplayAll;                              //单元格是否信息展示完全

    var tbClass = Opts.tableClass;                                   //表格样式
    var trMouseOverColor = Opts.mouseOverColor;                      //鼠标滑过样式
    var trMouseClickColor = Opts.mouseClickColor;                    //鼠标点击样式
    var trEvenRowColor = Opts.evenRowColor;                          //偶数行样式
    var trOddRowColor = Opts.oddRowColor;                            //奇数行样式

    //默认配置
    if (IsBlank(pageSize)) {
        pageSize = 20;
    }
    if (IsBlank(curPageNo)) {
        curPageNo = 1;
    }
    if (IsBlank(displayAll)) {
        displayAll = false;
    }
    //表格默认样式
    if (IsBlank(tbClass)) {
        tbClass = "jqtable";
    }
    if (IsBlank(trEvenRowColor)) {
        trEvenRowColor = "evenRowColor";
    }
    if (IsBlank(trOddRowColor)) {
        trOddRowColor = "oddRowColor";
    }
    if (IsBlank(trMouseOverColor)) {
        trMouseOverColor = "mouseOverColor";
    }
    if (IsBlank(trMouseClickColor)) {
        trMouseClickColor = "mouseClickColor";
    }
    var $table = $("#" + tableId);
    $($table).css("overflow", "auto");
    if (!IsBlank(divWidth)) {
        $($table).css("width", divWidth);
    }
    if (!IsBlank(divHeight)) {
        $($table).css("height", divHeight);
    }

    url += "?pageSize=" + pageSize + "&pageNo=" + curPageNo;

    if (!IsBlank(data)) {
        url += "&" + data;
    }

    var bBottom = true;
    var bHeader = true;
    var $pageBottom;
    var $pageHeader;
    if (typeof(pageHeader) == "undefined") {
        bHeader = false;
    } else {
        $pageHeader = $("#" + pageHeader);
        $($pageHeader).css("overflow", "auto");
        $($pageHeader).css("text-align", "right");
        if (!IsBlank(divWidth)) {
            $($pageHeader).css("width", divWidth);
        }
    }
    if (typeof(pageBottom) == "undefined") {
        bBottom = false;
    } else {
        $pageBottom = $("#" + pageBottom);
        $($pageBottom).css("overflow", "auto");
        $($pageBottom).css("text-align", "right");
        if (!IsBlank(divWidth)) {
            $($pageBottom).css("width", divWidth);
        }
    }

    Func($table, $pageHeader, $pageBottom, url, data, tbClass, tbColName, tbColModel, displayAll);

    curFunc = Func;

}
